package com.lepu.app.fun.my;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.app.utils.UmengEvent;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.UmengHelper;
import com.lepu.app.fun.index.bean.BeanIndexNew;
import com.lepu.app.fun.my.bean.BeanMsg;
import com.lepu.app.fun.my.bean.BeanMsgBbs;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.pasm.R;

public class MyMassegeActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener {
	
	private BeanMsg beanMsg = null;
	private TextView osText;
	private TextView fansText;
	private TextView bbsText;
	private TextView asdText;
	private TextView transText;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_massege_activity);

		init();
	}

	@Override
	public void onResume() {
		super.onResume();
		showBbsPoint();
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("消息");
		topbar.setonTopbarNewLeftLayoutListener(this);
		
		osText = (TextView) findViewById(R.id.osCountText);
		fansText = (TextView) findViewById(R.id.fansCountText);
		bbsText = (TextView) findViewById(R.id.bbsCountText);
		asdText = (TextView) findViewById(R.id.withCountText);
		transText = (TextView) findViewById(R.id.turnCountText);
		
		beanMsg = (BeanMsg) getIntent().getSerializableExtra("beanMsg");
		
		if(beanMsg != null){
			showPoint(beanMsg);
		}
		
		showBbsPoint();
		
	}
	
	//显示红点及数量
	public void showPoint(BeanMsg beanMsg){
		
		if(beanMsg.osCount != 0){
			osText.setVisibility(View.VISIBLE);
			osText.setText(String.valueOf(beanMsg.osCount));
		}
		
		if(beanMsg.fansCount != 0){
			fansText.setVisibility(View.VISIBLE);
			fansText.setText(String.valueOf(beanMsg.fansCount));
		}
		
		if(beanMsg.asdCount != 0){
			asdText.setVisibility(View.VISIBLE);
			asdText.setText(String.valueOf(beanMsg.asdCount));
		}
		
		if(beanMsg.transCount != 0){
			transText.setVisibility(View.VISIBLE);
			transText.setText(String.valueOf(beanMsg.transCount));
		}
	}
	
	public void showBbsPoint(){
		ArrayList<BeanMsgBbs> beanList = BeanMsgBbs.getMyMessage(LoginUtil.getUserId());
		int bbsCount = beanList.size();
		
		for(int i=0; i<beanList.size(); i++){
			if(BeanIndexNew.isExist(beanList.get(i).MessageID)){
				bbsCount = bbsCount-1;
			}
		}
		
		if(beanMsg != null){
			if(beanMsg.bbsCount > 0){
				bbsText.setVisibility(View.VISIBLE);
				bbsText.setText(String.valueOf(beanMsg.bbsCount));
			}else{
				bbsText.setVisibility(View.GONE);
			}
		}else{
			if(bbsCount > 0){
				bbsText.setVisibility(View.VISIBLE);
				bbsText.setText(String.valueOf(bbsCount));
			}else{
				bbsText.setVisibility(View.GONE);
			}
		}
		
	}

	public void viewClick(View view) {

		Intent intent = new Intent();

		int rid = view.getId();
		switch (rid) {
		case R.id.bbsLayout: //帖子动态

			if(LoginUtil.jumpLogin(this)){
				return;
			}

			intent.setClass(this, MyMassegeBbsDetailActivity.class);
			beanMsg = null;
			UmengHelper.CustomEvent(this, UmengEvent.event_xiaoxi_tiezidongtai);
			break;
		case R.id.osLayout:  //系统通知
			intent.setClass(this, MyMassegeDetailActivity.class);
			intent.putExtra("type", 1);
			TextView osText = (TextView) findViewById(R.id.osCountText);
			osText.setVisibility(View.GONE);
			UmengHelper.CustomEvent(this, UmengEvent.event_xiaoxi_xitongxiaoxi);
			break;
		case R.id.fansLayout:  //新的粉丝

			if(LoginUtil.jumpLogin(this)){
				return;
			}

			intent.setClass(this, MyMassegeDetailActivity.class);
			intent.putExtra("type", 2);
			TextView fansText = (TextView) findViewById(R.id.fansCountText);
			fansText.setVisibility(View.GONE);
			UmengHelper.CustomEvent(this, UmengEvent.event_xiaoxi_xindefensi);
			break;
		case R.id.withLayout:  //陪诊服务

			UmengHelper.CustomEvent(this, UmengEvent.event_xiaoxi_peizhenfuwu);

			if(LoginUtil.jumpLogin(this)){
				return;
			}

			intent.setClass(this, MyMassegeDetailActivity.class);
			intent.putExtra("type", 3);
			TextView withText = (TextView) findViewById(R.id.withCountText);
			withText.setVisibility(View.GONE);
			UmengHelper.CustomEvent(this, UmengEvent.event_xiaoxi_xitongxiaoxi);
			break;
		case R.id.turnLayout:  //转诊服务

			UmengHelper.CustomEvent(this, UmengEvent.event_xiaoxi_zhuanzhenfuwu);

			if(LoginUtil.jumpLogin(this)){
				return;
			}

			intent.setClass(this, MyMassegeDetailActivity.class);
			intent.putExtra("type", 4);
			TextView turnText = (TextView) findViewById(R.id.turnCountText);
			turnText.setVisibility(View.GONE);
			UmengHelper.CustomEvent(this, UmengEvent.event_xiaoxi_xindefensi);
			break;

		default:
			break;
		}
		startActivity(intent, true);
	}

}
